using System.Windows.Forms;
using Slippor.Douban.Radio.Assist;

namespace Slippor.Douban.Radio.BaseForm
{
    public class FormAutoDock
    {
        public const int DockTop = 18;
        public const int DockSide = 22;

        public static void SideHideOrShow(DoubanForm dockableForm, Timer dockTimer)
        {
            if (dockableForm.CurrentState == RadioState.Normal)
            {
                dockTimer.Stop();
                if (Cursor.Position.X > dockableForm.Left - 1 && Cursor.Position.X < dockableForm.Right &&
                    Cursor.Position.Y > dockableForm.Top - 1 && Cursor.Position.Y < dockableForm.Bottom)
                {
                    ResetDockForm(dockableForm);
                    dockTimer.Interval = 1500;
                }
                else if (dockableForm.FormTopMost)
                {
                    if (dockableForm.Top <= 4 && dockableForm.Left > 5 &&
                        dockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - dockableForm.Width)
                    {
                        dockableForm.OnDock(DockType.Top);
                        dockableForm.Top = DockTop + 1 - dockableForm.Height;
                        if (dockableForm.Left <= 4)
                        {
                            dockableForm.Left = -5;
                        }
                        else if (dockableForm.Left + dockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
                        {
                            dockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - dockableForm.Width + 5;
                        }
                    }
                    else if (dockableForm.Left <= 4)
                    {
                        dockableForm.OnDock(DockType.Left);
                        dockableForm.Left = DockSide + 1 - dockableForm.Width;
                        dockableForm.FormBorderStyle = FormBorderStyle.None;
                    }
                    else if (dockableForm.Left + dockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
                    {
                        dockableForm.OnDock(DockType.Right);
                        dockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockSide + 1;
                    }
                    dockTimer.Interval = 500;
                }
                dockTimer.Start();
            }
        }

        public static void ResetDockForm(DoubanForm dockableForm)
        {
            if (dockableForm.Top <= 0 && dockableForm.Left > 5 &&
                dockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - dockableForm.Width)
            {
                dockableForm.Top = 0;
                dockableForm.OnDock(DockType.None);
            }
            else if (dockableForm.Left <= 0)
            {
                dockableForm.Left = 0;
                dockableForm.OnDock(DockType.None);
            }
            else if (dockableForm.Left + dockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width)
            {
                dockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - dockableForm.Width;
                dockableForm.OnDock(DockType.None);
            }
            else
            {
                dockableForm.OnDock(DockType.None);
            }
        }
    }

    public enum DockType
    {
        None = 0,
        Left = 1,
        Right = 2,
        Top = 3,
    }
}